package com.srwl.mytx.utils;

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//



import android.content.Context;
import android.content.res.Resources;
import android.graphics.Rect;

public class ImageMsgPacker   {
    private static final int mMaxWidth = 200;
    private static final int mMaxHeight = 200;
    private static final int mMaxNeedServerToGiveThumnailWidth = 200;
    private static final int mMaxNeedServerToGiveThumnailHeight = 200;
    private static final int mMinWidth = 120;
    private static final int mMinHeight = 120;
    private static final int DEFAULT_WIDTH = 100;
    private static final int DEFAULT_HEIGHT = 100;
    private static int[] CDN_SIZE = new int[]{72, 100, 160, 200, 300};
    private static final int DEFAULT_GIF_WIDTH = 80;
    private static final int DEFAULT_GIF_HEIGHT = 80;
    private static int[] GIF_SIZE = new int[]{80, 90, 120};
    private float density;

    public ImageMsgPacker(Context context) {
        this.density = Resources.getSystem().getDisplayMetrics().density;
    }

    public Rect getOriImageSize() {
        return new Rect(0, 0, this.getDefaultWidth(), this.getDefaultHeight());
    }

    public Rect getPreImageSize(Rect oriImageSize) {
        int width = oriImageSize.width();
        int height = oriImageSize.height();
        Rect rect = new Rect(0, 0, width, height);
        if (width <= 0 || height <= 0) {
            width = this.getDefaultWidth();
            height = this.getDefaultHeight();
        }

        int[] resized = this.getResizedDimensionOfThumbnail(width, height, this.getMinWidth(), this.getMaxHeight());
        rect.right = resized[0];
        rect.bottom = resized[1];
        return rect;
    }

    public Rect getNeedServerToGivePreImageSize(Rect oriImageSize) {
        int width = oriImageSize.width();
        int height = oriImageSize.height();
        Rect rect = new Rect(0, 0, width, height);
        if (width <= 0 || height <= 0) {
            width = this.getDefaultWidth();
            height = this.getDefaultHeight();
        }

        int[] resized = this.getResizedDimensionOfThumbnail(width, height, this.getMinWidth(), this.getMaxNeedServerToGiveThumnailHeight());
        rect.right = resized[0];
        rect.bottom = resized[1];
        return rect;
    }

    public String getDefaultImageFormat() {
        return "jpg";
    }

    private int[] getResizedDimensionOfThumbnail(int actualWidth, int actualHeight, int minWidth, int maxHeight) {
        int[] resizedResults = new int[2];
        if (actualWidth <= actualHeight) {
            double ratio;
            int tmpHeight;
            if (actualHeight > maxHeight) {
                ratio = (double)actualHeight / (double)maxHeight;
                tmpHeight = (int)((double)actualWidth / ratio);
                if (tmpHeight > minWidth) {
                    resizedResults[0] = tmpHeight;
                    resizedResults[1] = maxHeight;
                } else {
                    ratio = (double)minWidth / (double)actualWidth;
                    int tmpHeight1 = (int)((double)actualHeight * ratio);
                    if (tmpHeight1 > maxHeight) {
                        resizedResults[0] = minWidth;
                        resizedResults[1] = maxHeight;
                    } else {
                        resizedResults[0] = minWidth;
                        resizedResults[1] = tmpHeight1;
                    }
                }
            } else if (actualWidth < minWidth) {
                ratio = (double)minWidth / (double)actualWidth;
                tmpHeight = (int)((double)actualHeight * ratio);
                if (tmpHeight > maxHeight) {
                    resizedResults[0] = minWidth;
                    resizedResults[1] = maxHeight;
                } else {
                    resizedResults[0] = minWidth;
                    resizedResults[1] = tmpHeight;
                }
            } else {
                resizedResults[0] = actualWidth;
                resizedResults[1] = actualHeight;
            }
        } else {
            int[] results = this.getResizedDimensionOfThumbnail(actualHeight, actualWidth, minWidth, maxHeight);
            resizedResults[0] = results[1];
            resizedResults[1] = results[0];
        }

        return resizedResults;
    }

    public int getDefaultWidth() {
        int width = (int)(100.0F * this.density);
        return this.getNearestInt(width);
    }

    public int getDefaultHeight() {
        int height = (int)(100.0F * this.density);
        return this.getNearestInt(height);
    }

    public int getMaxWidth() {
        int width = (int)(200.0F * this.density);
        return width;
    }

    public int getMaxHeight() {
        int height = (int)(200.0F * this.density);
        return height;
    }

    public int getMaxNeedServerToGiveThumnailWidth() {
        int width = (int)(200.0F * this.density);
        return width;
    }

    public int getMaxNeedServerToGiveThumnailHeight() {
        int height = (int)(200.0F * this.density);
        return height;
    }

    public int getMinWidth() {
        int width = (int)(120.0F * this.density);
        return width;
    }

    public int getMinHeight() {
        int height = (int)(120.0F * this.density);
        return height;
    }

    private int getNearestInt(int num) {
        int min = Math.abs(num - CDN_SIZE[0]);
        int j = 0;

        for(int i = 1; i < CDN_SIZE.length; ++i) {
            if (Math.abs(num - CDN_SIZE[i]) < min) {
                min = Math.abs(num - CDN_SIZE[i]);
                j = i;
            }
        }

        return CDN_SIZE[j];
    }

    private int getNearestGifSize(int num) {
        int min = Math.abs(num - GIF_SIZE[0]);
        int j = 0;

        for(int i = 1; i < GIF_SIZE.length; ++i) {
            if (Math.abs(num - GIF_SIZE[i]) < min) {
                min = Math.abs(num - GIF_SIZE[i]);
                j = i;
            }
        }

        return GIF_SIZE[j];
    }

    public int getDefaultGifWidth() {
        return this.getNearestGifSize(Math.max(this.getMinWidth(), 80));
    }

    public int getDefaultGifHeight() {
        return this.getNearestGifSize(Math.max(this.getMinHeight(), 80));
    }
}
